package com.zjgsu.nftplatform.controller;

import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.BlockchainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2022/3/6 15:19
 * @description
 */
@Api(tags = "区块链")
@RestController
@RequestMapping("/blockchain")
public class BlockchainController {

    private final BlockchainService blockchainService;

    public BlockchainController(BlockchainService blockchainService) {
        this.blockchainService = blockchainService;
    }

    @ApiOperation("作品内容")
    @GetMapping("/nft")
    public Result nft(@ApiParam("nft id") @RequestParam Long nftId) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.nft(nftId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("共创树")
    @GetMapping("/tree")
    public Result tree(@ApiParam("nft id") @RequestParam Long nftId) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.tree(nftId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("创建出版权协议")
    @PostMapping("/publication/create")
    public Result createPublication(@ApiParam("nft id") @RequestParam String nftId,
                                    @ApiParam("出版权期限（天）") @RequestParam String dayTime,
                                    @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.createPublication(nftId, dayTime, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查找出版权协议")
    @GetMapping("/publication/search")
    public Result searchPublication(@ApiParam("nft id") @RequestParam Long nftId) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.searchPublication(nftId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("设置分红")
    @PostMapping("/stake")
    public Result stake(@ApiParam("千分比") @RequestParam Integer ratio,
                        @ApiParam("出版权协议id") @RequestParam Long pid,
                        @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.stake(ratio, pid, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("设置价格")
    @PostMapping("/price")
    public Result price(@ApiParam("价格") @RequestParam Long price,
                        @ApiParam("出版权协议id") @RequestParam Long pid,
                        @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.price(price, pid, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看出版权协议")
    @GetMapping("/publication")
    public Result publication(@ApiParam("出版权协议id") @RequestParam Long pid) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.publication(pid));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("同意出版权协议")
    @PostMapping("/publication/agree")
    public Result agreePublication(@ApiParam("出版权协议id") @RequestParam Long pid,
                                   @ApiParam("价格") @RequestParam String price,
                                   @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, blockchainService.agreePublication(pid, price, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
